package goiot.api;

import com.fasterxml.jackson.core.type.TypeReference;

import goiot.GoiotHttp;
import goiot.dto.CmdInvokeDTO;
import goiot.dto.Device;
import goiot.dto.DeviceDetail;
import goiot.dto.JsonResp;
import goiot.dto.PageQuery;

/**
 * 设备相关API
 */
public class DeviceApi extends CurdApi<Device> {

	public DeviceApi() {
	}

	@Override
	protected String getFullPath(String path) {
		return "/api/device" + path;
	}

	/**
	 * 设备详情
	 * 
	 * @param deviceId
	 * @return
	 */
	public JsonResp<DeviceDetail> getDetail(String deviceId) {
		JsonResp<DeviceDetail> resp = GoiotHttp.get(getFullPath("/" + deviceId + "/detail"),
				new TypeReference<JsonResp<DeviceDetail>>() {
				});
		return resp;
	}

	/**
	 * 连接设备
	 * 
	 * @param deviceId
	 * @return
	 */
	public JsonResp<Void> connect(String deviceId) {
		JsonResp<Void> resp = GoiotHttp.post(getFullPath("/" + deviceId + "/connect"), VOID_RESP);
		return resp;
	}

	/**
	 * 断开连接
	 * 
	 * @param deviceId
	 * @return
	 */
	public JsonResp<Void> disconnect(String deviceId) {
		JsonResp<Void> resp = GoiotHttp.post(getFullPath("/" + deviceId + "/disconnect"), VOID_RESP);
		return resp;
	}

	/**
	 * 发布
	 * 
	 * @param deviceId
	 * @return
	 */
	public JsonResp<Void> deploy(String deviceId) {
		JsonResp<Void> resp = GoiotHttp.post(getFullPath("/" + deviceId + "/deploy"), VOID_RESP);
		return resp;
	}

	/**
	 * 取消发布
	 * 
	 * @param deviceId
	 * @return
	 */
	public JsonResp<Void> undeploy(String deviceId) {
		JsonResp<Void> resp = GoiotHttp.post(getFullPath("/" + deviceId + "/undeploy"), VOID_RESP);
		return resp;
	}

	/**
	 * 批量发布
	 * 
	 * @param deviceIds
	 * @return
	 */
	public JsonResp<Void> batchDeploy(String[] deviceIds) {
		JsonResp<Void> resp = GoiotHttp.post(getFullPath("/batch/deploy"), deviceIds, VOID_RESP);
		return resp;
	}

	/**
	 * 批量取消发布
	 * 
	 * @param deviceIds
	 * @return
	 */
	public JsonResp<Void> batchUndeploy(String[] deviceIds) {
		JsonResp<Void> resp = GoiotHttp.post(getFullPath("/batch/undeploy"), deviceIds, VOID_RESP);
		return resp;
	}

	/**
	 * 功能调用
	 * 
	 * @param deviceId
	 * @param data
	 * @return
	 */
	public JsonResp<Void> cmdInvoke(String deviceId, CmdInvokeDTO data) {
		JsonResp<Void> resp = GoiotHttp.post(getFullPath("/" + deviceId + "/invoke"), data, VOID_RESP);
		return resp;
	}

	/**
	 * 查询属性数据
	 * 
	 * @param deviceId
	 * @param data
	 * @return
	 */
	public JsonResp<Void> queryProperties(String deviceId, PageQuery data) {
		JsonResp<Void> resp = GoiotHttp.post(getFullPath("/" + deviceId + "/properties"), data, VOID_RESP);
		return resp;
	}

	/**
	 * 查询日志
	 * 
	 * @param deviceId
	 * @param data
	 * @return
	 */
	public JsonResp<Void> queryLogs(String deviceId, PageQuery data) {
		JsonResp<Void> resp = GoiotHttp.post(getFullPath("/" + deviceId + "/logs"), data, VOID_RESP);
		return resp;
	}

	/**
	 * 查询时间
	 * 
	 * @param deviceId
	 * @param eventId
	 * @param data
	 * @return
	 */
	public JsonResp<Void> queryEvent(String deviceId, String eventId, PageQuery data) {
		JsonResp<Void> resp = GoiotHttp.post(getFullPath("/" + deviceId + "/event/" + eventId), data, VOID_RESP);
		return resp;
	}
}
